from PyQt4.QtCore import *
from PyQt4.QtGui import *

import scene
import ops
import arithmeticOps
import inputOps
import outputOps

from mlayout import Ui_Dialog
class GraphDialog(QMainWindow,QDialog,Ui_Dialog):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)


        QObject.connect(self.actionUndo,SIGNAL("triggered()"),self.undoLast)
        QObject.connect(self.actionRedo,SIGNAL("triggered()"),self.redoLast)


        self.scene = scene.modelScene()
        self.graphicsView.setScene(self.scene)
        self.statusBar = QStatusBar()
        self.scene.statusBar = self.statusBar
        self.setStatusBar(self.statusBar)

        QObject.connect(self.menu_Run, SIGNAL("aboutToShow()"),self.checkRun)

        QObject.connect(self.actionDump_Graph,SIGNAL("triggered()"), self.scene.dumpGraph)
        QObject.connect(self.actionDump_TopoSort,SIGNAL("triggered()"), self.scene.dumpSort)
        QObject.connect(self.actionCheck_Cycles,SIGNAL("triggered()"), self.scene.has_cycle)

        QObject.connect(self.actionRun_process,SIGNAL("triggered()"), self.run_process)
        QObject.connect(self.actionClear_Errors,SIGNAL("triggered()"),self.scene.clearErrors)
        
        self.opButtons=[]
        addOpButtons(self,arithmeticOps,self.arithmeticPage)
        addOpButtons(self,inputOps,self.inputsPage)
        addOpButtons(self,outputOps,self.outputsPage)

    def undoLast(self):
        self.scene.undoStack.undo()
    def redoLast(self):
        self.scene.undoStack.redo()

    def run_process(self):
        self.scene.run_process()
    def checkRun(self):
        if len(self.scene.errors) > 0:
            self.actionRun_process.setDisabled(True)
        else:
            self.actionRun_process.setEnabled(True)
            
def addOpButtons(dialog, module, page):
    for klass in module.ops:
        button = ops.OpsButton(klass,dialog.scene,page)
        dialog.opButtons.append(button)
        page.layout().addWidget(button)
    spacerItem = QSpacerItem(20,40,QSizePolicy.Minimum,QSizePolicy.Expanding)
    page.layout().addItem(spacerItem)
    

def main(args):
    app = QApplication(args)
    mainWindow=GraphDialog()
    mainWindow.show()
    app.exec_()

    
import sys
if __name__=="__main__":
    main(sys.argv)
